 /**
 * Description: 物资类别
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.sys.controller;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.common.beans.constant.DicTypeEnum;
import com.metro.paperless.common.utils.DicUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

import com.metro.paperless.sys.service.inf.ISysMaterialsTypeService;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.web.controller.BaseController;
 /**
 * 物资类别<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Controller
@RequestMapping("/sysMaterialsType")
public class SysMaterialsTypeController extends BaseController {
    //菜单编码
    private static final String menuCode = "SYS_MATERIALS_TYPE";
    //模块描述
    private static final String MODULE_DESC = "物资类别";
    @Autowired
    private ISysMaterialsTypeService sysmaterialstypeService;
    @Override
    protected String getViewDir() {
        return "sys/";
    }
    
    /**
     * 去查询物资类别
     * 
     * @param sysmaterialstype 物资类别页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资类别列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(SysMaterialsType sysmaterialstype) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("sysmaterialstype_list"));
            mav.addObject("searchEntity", sysmaterialstype);
        } catch (Exception ex) {
            log.error("查询物资类别出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询物资类别
     *
     * @param sysmaterialstype 物资类别页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资类别列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(SysMaterialsType sysmaterialstype, Page page, QueryParams<SysMaterialsType> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(sysmaterialstype);
            PageInfo<SysMaterialsType> pageInfo = sysmaterialstypeService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询物资类别出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增物资类别
     * 
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增物资类别")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增物资类别出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改物资类别所需参数
     * @author Hunter
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("action", action);
        mav.setViewName(getView("sysmaterialstype_form"));
    }
    
    /**
     * 新增物资类别
     * 
     * @param entity 物资类别页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增物资类别")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysMaterialsType(SysMaterialsType entity, HttpServletRequest request,String type_name) {
        JsonResult jr = new JsonResult();
        entity.setTypeName(type_name);
        //判断类别是否存在
        IResult result = sysmaterialstypeService.checkMaterialsType(entity);
    	String code = result.getCode();
    	 if ( code.equals(ResultCode.DUPLICATE_KEY.getCode())) {
        	 jr.setResult(result);
             log.debug("物资类别已存在【{}】", entity);
    	 }else {
    		 try {
    	            IResult res = sysmaterialstypeService.saveMaterialsType(entity);
    	            DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE);
    	            DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE_HIDDEN);
    	            jr.setResult(res);
    	            log.debug("新增物资类别成功【{}】", entity);
    	        } catch (Exception ex) {
    	            log.error("新增物资类别出错", ex);
    	            jr.setResult(ResultCode.ERROR);
    	        }
    	 }
        return jr;
    }

    /**
     * 删除物资类别
     * 
     * @param id 物资类别页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除物资类别")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除物资类别id={}",id);
            IResult codeMsg = sysmaterialstypeService.deleteByIds(id);
            DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除物资类别出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改物资类别
     * 
     * @param id 物资类别页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改物资类别")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            SysMaterialsType entity = sysmaterialstypeService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改物资类别", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改物资类别
     * 
     * @param entity 物资类别页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改物资类别")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysMaterialsType(SysMaterialsType entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        IResult result = sysmaterialstypeService.checkMaterialsType(entity);
    	String code = result.getCode();
        if ( code.equals(ResultCode.DUPLICATE_KEY.getCode())) {
       	 	jr.setResult(result);
            log.debug("物资类别已存在【{}】", entity);
   	 	}else {
	        try {
	        		IResult res = sysmaterialstypeService.update(entity);
	        		DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE);
	        		DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE_HIDDEN);
	        		jr.setResult(res);
	        		log.debug("修改物资类别成功【{}】", entity);
	        	} catch (Exception ex) {
	        		log.error("修改物资类别出错", ex);
	        		jr.setResult(ResultCode.ERROR);
	        	}
   	 		}
        return jr;
    }
    
    /**
     * 去查看物资类别
     * 
     * @param id 物资类别页面表单对象唯一标识
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看物资类别详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", sysmaterialstypeService.getById(id));
            mav.setViewName(getView("sysmaterialstype_info"));
        } catch (Exception ex) {
            log.error("查看物资类别出错", ex);
        }
        return mav;
        
    }
    
}
